<?php

/**
 * story actions.
 *
 * @package    sf_sandbox
 * @subpackage story
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class storyActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $c = new Criteria();
    $c->add(MaterialPeer::TYPE_ID,10);
            if($this->getUser()->hasCredential('partner') and !$this->getUser()->isSuperadmin() )
            {
                $c->add(MaterialPeer::USER_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
               // $c->addJoin(sfGuardUserPeer::ID,MaterialPeer::USER_ID);
            }
               if($request->getParameter('pub')=='true')
                {
                   $c->add(MaterialPeer::PUB, true);
                }
               if($request->getParameter('pub')=='false')
                {
                   $c->add(MaterialPeer::PUB, false);
                }

       if(!$request->getParameter('page')){ $this->page = 1;}
        else { $this->page=$request->getParameter('page');}

        $pager = new sfPropelPager ( 'Material', 10 );
        $pager->setCriteria ( $c );
        $pager->setPeerMethod ( 'doSelect' );
        $pager->setPage ( $this->page );
        $pager->init ();
        $this->material_list = $pager;

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new storyForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new storyForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($material = MaterialPeer::retrieveByPk($request->getParameter('id')), sprintf('Object material does not exist (%s).', $request->getParameter('id')));
    $this->form = new storyForm($material);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($material = MaterialPeer::retrieveByPk($request->getParameter('id')), sprintf('Object material does not exist (%s).', $request->getParameter('id')));
    $this->form = new storyForm($material);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($material = MaterialPeer::retrieveByPk($request->getParameter('id')), sprintf('Object material does not exist (%s).', $request->getParameter('id')));
    $material->delete();

    $this->redirect('story/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $material = $form->save();
       $this->getUser ()->setFlash ( 'notice', 'Данные сохранены.' );
      $this->redirect('story/edit?id='.$material->getId());
    }
  }
}
